package liuliu.throughwaring.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

import net.tsz.afinal.annotation.view.CodeNote;

import java.io.File;

import liuliu.custom.control.edittext.HomeTypeEditText;
import liuliu.custom.control.edittext.ImageEditText;
import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.control.add.police.company.IRegUnitView;
import liuliu.throughwaring.control.add.police.company.RegUnitListener;
import liuliu.throughwaring.model.CompanyModel;
import liuliu.throughwaring.ui.map.DingWeiListener;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;

/**
 * 注册出租屋信息
 * Created by Administrator on 2016/5/12.
 */
public class RegChuzuwuActivity extends BaseActivity implements IRegUnitView {
    public static RegChuzuwuActivity mInstance;
    @CodeNote(id = R.id.home_type_iet)
    HomeTypeEditText home_type_iet;
    @CodeNote(id = R.id.main_toolbar)
    TToolbar main_toolbar;
    @CodeNote(id = R.id.xiaoqu_name_iet)
    ImageEditText xiaoqu_name_iet;
    @CodeNote(id = R.id.xiaoqu_address_iet)
    ImageEditText xiaoqu_address_iet;
    @CodeNote(id = R.id.menpai_num_iet)
    ImageEditText menpai_num_iet;
    @CodeNote(id = R.id.fang_name_iet)
    ImageEditText fang_name_iet;
    @CodeNote(id = R.id.idcard_iet)
    ImageEditText idcard_iet;
    @CodeNote(id = R.id.tel_iet)
    ImageEditText tel_iet;
    @CodeNote(id = R.id.remark_iet)
    ImageEditText remark_iet;
    @CodeNote(id = R.id.reg_unit_btn, click = "onClick")
    Button reg_unit_btn;
    @CodeNote(id = R.id.reg_unit_btn_add, click = "onClick")
    Button reg_unit_btn_add;
    @CodeNote(id = R.id.reg_unit_img, click = "onClick")
    ImageView reg_unit_img;
    String mPath;//图片地址
    RegUnitListener mListener;
    boolean saveAndadd = false;//保存并添加，true.false只是保存
    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明定位回调监听器
    public AMapLocationListener mLocationListener = new DingWeiListener();
    //声明mLocationOption对象
    public AMapLocationClientOption mLocationOption = null;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_reg_chuzuwu);
        mInstance = this;
        mListener = new RegUnitListener(this, this);
    }

    @Override
    public void initEvents() {
        main_toolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                mInstance.finish();
            }
        });
        // 启动定位,还是从缓存取值
        LocationStart();
        if (Utils.ReadString("NowLocation").equals("")) {
            xiaoqu_address_iet.setText("河北省保定市竞秀区东风中路");
        } else {
            xiaoqu_address_iet.setText(Utils.ReadString("NowLocation"));
        }

    }

    //高德地图定位
    public void LocationStart() {
        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        mLocationOption.setOnceLocation(true);
        //设置是否强制刷新WIFI，默认为强制刷新
        mLocationOption.setWifiActiveScan(true);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setMockEnable(false);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.reg_unit_btn_add://保存并继续添加按钮
                if (checkText()) {
                    CompanyModel model = getText();
                    model.setIsPush("null");
                    mListener.RegUnit(model);
                    saveAndadd = true;
                }
                break;
            case R.id.reg_unit_btn://保存按钮
                if (checkText()) {
                    CompanyModel model = getText();
                    model.setIsPush("null");
                    mListener.RegUnit(model);
                    saveAndadd = false;
                }
                break;
            case R.id.reg_unit_img://调用摄像头
                Intent intent = new Intent(this, MultiImageSelectorActivity.class);
                intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);// 是否显示拍摄图片
                intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 1);// 最大可选择图片数量
                intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_SINGLE);// 选择模式
                intent.putExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, mPath);// 默认选择
                startActivityForResult(intent, 110);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 110) {
            if (resultCode == RESULT_OK) {
                mPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT).get(0);
                File file = new File(mPath);
                if (file.exists()) {
                    Bitmap bm = BitmapFactory.decodeFile(mPath);
                    if (bm != null) {
                        reg_unit_img.setImageBitmap(Utils.centerSquareScaleBitmap(bm, 60));
                    }
                }
            }
        }
    }

    /**
     * 获得输入框中所有输入的内容
     */
    private CompanyModel getText() {
        CompanyModel model = new CompanyModel();
        model.setCompanyType("Lease");
        model.setCompanyName(xiaoqu_name_iet.getText());
        model.setCompanyAddress(xiaoqu_address_iet.getText());
        model.setHMenpai(menpai_num_iet.getText());
        model.setCompanyLegalPerson(fang_name_iet.getText());
        model.setHIdcard(idcard_iet.getText());
        model.setCompanyPhoneNumber(tel_iet.getText());
        model.setCompanyComment(remark_iet.getText());
        model.setCompanyOveralImageUrl(mPath);
        model.setHHuxing(home_type_iet.getHouseStyle());
        return model;
    }

    /**
     * 验证必填项是否补全,没有录入显示对应提示
     *
     * @return 全部填写返回true.否则返回false.
     */
    private boolean checkText() {
        if (xiaoqu_name_iet.getText().equals("")) {
            mUtils.ToastShort("小区名称不能为空");
            return false;
        }
        if (xiaoqu_address_iet.getText().equals("")) {
            mUtils.ToastShort("小区地址不能为空");
            return false;
        }
        if (menpai_num_iet.getText().equals("")) {
            mUtils.ToastShort("门牌号码不能为空");
            return false;
        }
        if (fang_name_iet.getText().equals("")) {
            mUtils.ToastShort("房主姓名不能为空");
            return false;
        }
        if (idcard_iet.getText().equals("")) {
            mUtils.ToastShort("房主证件号码不能为空");
            return false;
        }
        if (tel_iet.getText().equals("")) {
            mUtils.ToastShort("联系方式不能为空");
            return false;
        }
        int length = home_type_iet.getHouseStyle().length() - home_type_iet.getHouseStyle().replace("0", "").length();//计算出有几个0.
        if (length == 3) {
            mUtils.ToastShort("户型输入有问题,请重新输入");
            return false;
        }
        return true;
    }

    /**
     * 清空文本内容
     */
    private void clearText() {
        xiaoqu_name_iet.setText("");
        xiaoqu_address_iet.setText("");
        menpai_num_iet.setText("");
        fang_name_iet.setText("");
        idcard_iet.setText("");
        tel_iet.setText("");
        remark_iet.setText("");
        home_type_iet.clearHouseStyle();
    }

    /**
     * 通知后台保存
     *
     * @param isTrue
     * @param mes
     */
    @Override
    public void onRegUnitResult(boolean isTrue, String mes) {
        if (isTrue) {
            if (saveAndadd) {//保存并继续添加
                clearText();
            } else {//添加成功关闭页面
                setResult(131, new Intent());
                this.finish();
            }
        }
        mUtils.ToastShort("添加成功");
    }
}
